package com.example.serverdemo.bean;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.example.serverdemo.bean.json.device.JsonRootBean;
import com.example.serverdemo.common.Constant;
import com.example.serverdemo.utils.crcutls.CRCUtil;
import com.example.serverdemo.utils.log.PrintLogUtil;
import com.example.serverdemo.utils.stringutils.StringUtils;

import java.util.Date;

public class DeviceInfoBean {

    //id
    //固件版本号
    //设备型号
    //设备名称
    //设备地址

    private String deviceId;
    private String deviceFirmwareVersion;
    private int deviceTypeId;
    private String deviceName;
    private int deviceAddress;
    private Date deviceRegisterTime;
    private Date deviceLastConnectedTime;
    private boolean deviceConnectStatus;

    public static DeviceInfoBean build(byte[] data){
        DeviceInfoBean deviceInfoBean = null;
        try {
            if (data[0] == Constant.FLAG1 && data[1] == Constant.FLAG2 && data[2] == Constant.SERVER_ADDRESS){
                byte[] crcResource = data.clone();
                crcResource[6] = Constant.DEFAULT_CRC_PLACEHOLDER;
                crcResource[7] = Constant.DEFAULT_CRC_PLACEHOLDER;
                byte[] crc = Convert.intToBytes(CRCUtil.getInstance().calcCrc16(crcResource));
                if (crc[3] == data[6] && crc[2] == data[7]){
                    byte[] deviceInfo = new byte[data.length - 8];
                    System.arraycopy(data,8,deviceInfo,0,deviceInfo.length);
                    String jsonString = new String(deviceInfo,"GBK");
                    JsonRootBean jsonBean = JSON.parseObject(jsonString, JsonRootBean.class);
                    deviceInfoBean = jsonBean.getFormat().toDeviceInfoBean();
                }
            }
        }catch (Exception e){
            PrintLogUtil.printError(DeviceInfoBean.class.getName(),e);
        }
        return deviceInfoBean;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getDeviceFirmwareVersion() {
        return deviceFirmwareVersion;
    }

    public void setDeviceFirmwareVersion(String deviceFirmwareVersion) {
        this.deviceFirmwareVersion = deviceFirmwareVersion;
    }

    public int getDeviceTypeId() {
        return deviceTypeId;
    }

    public void setDeviceTypeId(int deviceTypeId) {
        this.deviceTypeId = deviceTypeId;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public int getDeviceAddress() {
        return deviceAddress;
    }

    public void setDeviceAddress(int deviceAddress) {
        this.deviceAddress = deviceAddress;
    }

    public Date getDeviceRegisterTime() {
        return deviceRegisterTime;
    }

    public void setDeviceRegisterTime(Date deviceRegisterTime) {
        this.deviceRegisterTime = deviceRegisterTime;
    }

    public Date getDeviceLastConnectedTime() {
        return deviceLastConnectedTime;
    }

    public void setDeviceLastConnectedTime(Date deviceLastConnectedTime) {
        this.deviceLastConnectedTime = deviceLastConnectedTime;
    }

    public boolean isDeviceConnectStatus() {
        return deviceConnectStatus;
    }

    public void setDeviceConnectStatus(boolean deviceConnectStatus) {
        this.deviceConnectStatus = deviceConnectStatus;
    }
}
